<?php
/**
 * @author GhostWei
 * @date Sep 21, 2012
 */
class front_controller_uc extends front_controller_panel {

	protected $referer = null;
	public function init() {
		if(!parent::init() || $this->login()) {
			return;
		}
		if($this->view->referer === null) {
			$this->view->referer = $this->getRequest()->getHeader('referer');
		}
		$this->referer = $this->view->referer;
		$this->layout()->setLayout('layout/uc');
		$this->_crumbs()->addPage(system_nav_page::create($this->language('用户中心'))->setRoute('account/index'));
	}
	
	protected function _saveToDatabase(system_concept_row $row, $upload, $api, $rigorous = true, $saveParams = null) {
		try {
			$this->referer = $this->_post('referer');
			system_db::getAdapter()->beginTransaction();
			if($this->_post('delete')) {
				$row->delete();
			} else {
				if($this->_post('deleteResource')) {
					$row->set($upload, '');
				} else {
					if(isset($_FILES[$upload]) && is_uploaded_file($_FILES[$upload]['tmp_name'])) {
						$class = 'resource_api_' . strtolower($api);
						$rigorous = $upload == 'cover' ? false : $rigorous;
						$_api = new $class();
						if(1 && $watermark = $this->_post('watermark')) {
							$_api->setWatermark($watermark);
						}
						$res = $_api->create($_FILES[$upload], $rigorous);
						if($res) {
							$row->set($upload, $res->getHash());
						}
						$res->save();
					}
				}
				$row = $row->save($saveParams);
			}
			system_db::getAdapter()->commit();
			$this->view->throwMessage = 'Complete';
		} catch (Exception $e) {
			system_db::getAdapter()->rollBack();
			$this->view->throwException = $e;
		}
		$this->view->referer = $this->referer;
		return $row;
	}

}
